#include <chrono>
#include <future>
#include <iostream>

int main() {
  std::promise<void> ready_promise, t1_ready_promise, t2_ready_promise;
  std::shared_future<void> ready_future(ready_promise.get_future());

  std::chrono::time_point<std::chrono::high_resolution_clock> start;

  auto fun1 = [&, ready_future]() -> std::chrono::duration<double, std::milli> {
    std::cout << "func1 Thread ID = " << std::this_thread::get_id() << "\n";
    t1_ready_promise.set_value();
    std::cout << "fn1 shared_future start\n";
    ready_future.wait(); // 等待来自 main() 的信号
    return std::chrono::high_resolution_clock::now() - start;
  };

  auto fun2 = [&, ready_future]() -> std::chrono::duration<double, std::milli> {
    std::cout << "func2 Thread ID = " << std::this_thread::get_id() << "\n";
    t2_ready_promise.set_value();
    std::cout << "fn2 shared_future start\n";
    ready_future.wait(); // 等待来自 main() 的信号
    return std::chrono::high_resolution_clock::now() - start;
  };

  auto result1 = std::async(std::launch::async, fun1);
  auto result2 = std::async(std::launch::async, fun2);
  // 等待线程变为就绪
  t1_ready_promise.get_future().wait();
  t2_ready_promise.get_future().wait();

  // 线程已就绪，开始时钟
  start = std::chrono::high_resolution_clock::now();

  std::cout << "set promise value\n";
  // 通知两个线程开始
  ready_promise.set_value();

  std::cout << "Thread 1 received the signal " << result1.get().count()
            << " ms after start\n"
            << "Thread 2 received the signal " << result2.get().count()
            << " ms after start\n";
}
